<template>
  <ul class="app-header-nav">
    <li
      class="home"
      @click="currentIdx = 'home'"
      :class="{ active: currentIdx == 'home' }"
    >
      <RouterLink to="/">首页</RouterLink>
    </li>
    <li
      v-for="(item, idx) in list"
      :key="item.id"
      @mouseleave="hide(item)"
      @mousemove="show(item)"
    >
      <RouterLink
        :class="{ active: currentIdx == idx }"
        :to="`/category/${item.id}`"
        @click="hide(item, idx)"
      >
        {{ item.name }}
      </RouterLink>
      <div class="layer" :class="{ open: item.open }">
        <!-- 二级 -->
        <ul>
          <li v-for="sub in item.children" :key="sub.id">
            <RouterLink
              :to="`/category/sub/${sub.id}`"
              @click="hide(item, idx)"
            >
              <img :src="sub.picture" />
              <p>{{ sub.name }}</p>
            </RouterLink>
          </li>
        </ul>
      </div>
    </li>
  </ul>
</template>

<script setup>
import { useStore } from 'vuex'
import { computed, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
const store = useStore()
const list = computed(() => {
  return store.state.cartgory.list
})

let currentIdx = ref('')
const route = useRoute()
const show = (item) => {
  store.commit('cartgory/show', item)
}
const hide = (item, idx) => {
  if (idx || idx == 0) {
    currentIdx.value = idx
  }
  store.commit('cartgory/hide', item)
}
let navName = {
  '/': 'home',
  '/category/1005000': 0,
  '/category/1005002': 1,
  '/category/1010000': 2,
  '/category/1011000': 3,
  '/category/1013001': 4,
  '/category/1019000': 5,
  '/category/1043000': 6,
  '/category/109243029': 7,
}
watch(
  () => route.path,
  (n) => {
    currentIdx.value = navName[n]
  },
  { immediate: true },
)
</script>

<style scoped lang="less">
.app-header-nav {
  width: 820px;
  display: flex;
  justify-content: space-around;
  padding-left: 40px;
  position: relative;
  z-index: 998;
  .active {
    color: @xtxColor;
    border-bottom: 1px solid @xtxColor;
  }
  > li {
    margin-right: 40px;
    width: 38px;
    text-align: center;
    > a {
      font-size: 16px;
      line-height: 32px;
      height: 32px;
      display: inline-block;
    }
    &:hover {
      > a {
        color: @xtxColor;
        border-bottom: 1px solid @xtxColor;
      }
      // .layer {
      //   height: 132px;
      //   opacity: 1;
      // }
    }
  }
}

// 子分类
.layer {
  &.open {
    height: 132px;
    opacity: 1;
  }
  width: 1240px;
  background-color: #fff;
  position: absolute;
  left: -200px;
  top: 56px;
  height: 0;
  overflow: hidden;
  opacity: 0;
  box-shadow: 0 0 5px #ccc;
  transition: all 0.2s 0.1s;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 70px;
    align-items: center;
    height: 132px;
    li {
      width: 110px;
      text-align: center;
      img {
        width: 60px;
        height: 60px;
      }
      p {
        padding-top: 10px;
      }
      &:hover {
        p {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>
